package application;

import javax.swing.*;
import network.*;
import javax.swing.JComboBox;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;

public class MainWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTextArea jTextArea = null;
	private JButton sendButton = null;
	private JList targetList = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JButton refreshTargetsButton = null;
	private JComboBox jComboBox = null;
	private JScrollPane jScrollPane = null;
	private JScrollPane jScrollPane1 = null;
	private JSlider packetSizeSlider = null;
	private JLabel jLabel2 = null;
	private JLabel packetSizeLabel = null;
	private JRadioButton targetListRadioButton = null;
	private JRadioButton targetNameRadioButton = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JTextField targetNameTextField = null;
	private JLabel jLabel5 = null;
	private JLabel bufferSizeLabel = null;
	private long maxSize = 0;
	private long maxPackets = 0;
	private JLabel jLabel6 = null;
	private JLabel maxBufSizeLabel = null;
	private JLabel jLabel7 = null;
	private JLabel targetsLabel = null;
	private JLabel jLabel8 = null;
	private JLabel packetsLabel = null;
	private JLabel jLabel81 = null;
	private JLabel maxPacketsLabel = null;
	private JButton resetButton = null;

	/**
	 * This is the default constructor
	 */
	public MainWindow() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(793, 497);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.out.println("windowClosing()"); // TODO Auto-generated
				// Event stub
				// windowClosing()
				System.exit(0);
			}
		});
		this.setContentPane(getJContentPane());
		this.setTitle(ApplicationLayer.myID
				+ " - Disruption Tolerant Networks - Evaluation Program");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			maxPacketsLabel = new JLabel();
			maxPacketsLabel.setBounds(new Rectangle(664, 427, 91, 23));
			maxPacketsLabel.setText("0");
			jLabel81 = new JLabel();
			jLabel81.setBounds(new Rectangle(540, 427, 120, 21));
			jLabel81.setText("Max Packets Stored");
			packetsLabel = new JLabel();
			packetsLabel.setBounds(new Rectangle(677, 399, 78, 23));
			packetsLabel.setText("0");
			jLabel8 = new JLabel();
			jLabel8.setBounds(new Rectangle(571, 396, 101, 26));
			jLabel8.setText("Packets Stored");
			targetsLabel = new JLabel();
			targetsLabel.setBounds(new Rectangle(659, 368, 95, 22));
			targetsLabel.setText("0");
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(570, 367, 83, 24));
			jLabel7.setText("Destinations");
			maxBufSizeLabel = new JLabel();
			maxBufSizeLabel.setBounds(new Rectangle(634, 340, 107, 22));
			maxBufSizeLabel.setText("0");
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(570, 340, 58, 21));
			jLabel6.setText("MAX:");
			bufferSizeLabel = new JLabel();
			bufferSizeLabel.setBounds(new Rectangle(569, 310, 175, 27));
			bufferSizeLabel.setText("0");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(570, 283, 177, 23));
			jLabel5.setText("Bundle Buffer size");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(598, 218, 150, 24));
			jLabel4.setText("Specify target name");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(598, 196, 159, 21));
			jLabel3.setText("Choose target from the list");
			packetSizeLabel = new JLabel();
			packetSizeLabel.setBounds(new Rectangle(570, 151, 93, 15));
			packetSizeLabel.setText("JLabel");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(564, 106, 84, 14));
			jLabel2.setText("Packet Size");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(175, 414, 75, 16));
			jLabel1.setText("Text to Send");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(458, 406, 40, 20));
			jLabel.setText("Target");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getSendButton(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getRefreshTargetsButton(), null);
			jContentPane.add(getJComboBox(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJScrollPane1(), null);
			jContentPane.add(getPacketSizeSlider(), null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(packetSizeLabel, null);
			jContentPane.add(getTargetListRadioButton(), null);
			jContentPane.add(getTargetNameRadioButton(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(jLabel4, null);
			jContentPane.add(getTargetNameTextField(), null);
			jContentPane.add(jLabel5, null);
			jContentPane.add(bufferSizeLabel, null);
			jContentPane.add(jLabel6, null);
			jContentPane.add(maxBufSizeLabel, null);
			jContentPane.add(jLabel7, null);
			jContentPane.add(targetsLabel, null);
			jContentPane.add(jLabel8, null);
			jContentPane.add(packetsLabel, null);
			jContentPane.add(jLabel81, null);
			jContentPane.add(maxPacketsLabel, null);
			jContentPane.add(getResetButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

	/**
	 * This method initializes sendButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getSendButton() {
		if (sendButton == null) {
			sendButton = new JButton();
			sendButton.setBounds(new Rectangle(660, 15, 91, 47));
			sendButton.setText("SEND");
			sendButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					send();
				}
			});
		}
		return sendButton;
	}

	/**
	 * This method initializes targetList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getTargetList() {
		if (targetList == null) {
			targetList = new JList();
		}
		return targetList;
	}

	/**
	 * This method initializes refreshTargetsButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getRefreshTargetsButton() {
		if (refreshTargetsButton == null) {
			refreshTargetsButton = new JButton();
			refreshTargetsButton.setBounds(new Rectangle(555, 13, 90, 42));
			refreshTargetsButton.setText("Refresh Target List");
			refreshTargetsButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							updateTargetList();
						}
					});
		}
		return refreshTargetsButton;
	}

	private void updateTargetList() {
		targetList.setListData(NetworkLayer.getTargetList());
	}

	private void send() {
		System.out.println(jComboBox.getSelectedIndex());
		if (targetListRadioButton.isSelected()) {
			if ((String) targetList.getSelectedValue() != null) {
				ApplicationLayer.send(jTextArea.getText(), (String) targetList
						.getSelectedValue(), jComboBox.getSelectedIndex());
			}
		} else if (targetNameRadioButton.isSelected()) {
			if (targetNameTextField.getText() != "") {
				ApplicationLayer.send(jTextArea.getText(), targetNameTextField
						.getText(), jComboBox.getSelectedIndex());
			}
		}
	}

	/**
	 * This method initializes jComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			String[] set = { "Best Effort", "ACK", "Sliding Window" };
			jComboBox = new JComboBox(set);
			jComboBox.setBounds(new Rectangle(663, 75, 90, 27));
		}
		return jComboBox;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(30, 16, 360, 379));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jScrollPane1
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(414, 16, 129, 378));
			jScrollPane1.setViewportView(getTargetList());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes packetSizeSlider
	 * 
	 * @return javax.swing.JSlider
	 */
	private JSlider getPacketSizeSlider() {
		if (packetSizeSlider == null) {
			packetSizeSlider = new JSlider();
			packetSizeSlider.setMinimum(256);
			packetSizeSlider.setMaximum(ApplicationLayer.MAXPACKETSIZE);
			packetSizeSlider.setValue(ApplicationLayer.PACKETSIZE);
			packetSizeLabel.setText(Integer.toString(packetSizeSlider
					.getValue()));
			packetSizeSlider.setMinorTickSpacing(5000);
			packetSizeSlider.setMajorTickSpacing(10000);
			packetSizeSlider.setPaintTicks(true);
			packetSizeSlider.setBounds(new Rectangle(558, 121, 193, 17));
			packetSizeSlider
					.addChangeListener(new javax.swing.event.ChangeListener() {
						public void stateChanged(javax.swing.event.ChangeEvent e) {
							ApplicationLayer.PACKETSIZE = packetSizeSlider
									.getValue();
							packetSizeLabel.setText(Integer
									.toString(packetSizeSlider.getValue()));
						}
					});
		}
		return packetSizeSlider;
	}

	/**
	 * This method initializes targetListRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getTargetListRadioButton() {
		if (targetListRadioButton == null) {
			targetListRadioButton = new JRadioButton();
			targetListRadioButton.setSelected(true);
			targetListRadioButton.setBounds(new Rectangle(570, 196, 21, 21));
			targetListRadioButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							targetNameRadioButton.setSelected(false);
							targetListRadioButton.setSelected(true);
						}
					});
		}
		return targetListRadioButton;
	}

	/**
	 * This method initializes targetNameRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getTargetNameRadioButton() {
		if (targetNameRadioButton == null) {
			targetNameRadioButton = new JRadioButton();
			targetNameRadioButton.setSelected(false);
			targetNameRadioButton.setBounds(new Rectangle(570, 216, 24, 23));
			targetNameRadioButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							targetNameRadioButton.setSelected(true);
							targetListRadioButton.setSelected(false);
						}
					});
		}
		return targetNameRadioButton;
	}

	/**
	 * This method initializes targetNameTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTargetNameTextField() {
		if (targetNameTextField == null) {
			targetNameTextField = new JTextField();
			targetNameTextField.setBounds(new Rectangle(585, 247, 167, 29));
		}
		return targetNameTextField;
	}

	public void setBufferSizeLabel(long size, long targets, long storedPackets) {
		if (size > maxSize) {
			maxSize = size;
			maxBufSizeLabel.setText(Long.toString(size / 1000) + " kb");
		}
		bufferSizeLabel.setText(Long.toString(size / 1000) + " kb");
		targetsLabel.setText(Long.toString(targets));
		if (storedPackets > maxPackets) {
			maxPackets = storedPackets;
			maxPacketsLabel.setText(Long.toString(storedPackets));
		}
		packetsLabel.setText(Long.toString(storedPackets));
	}

	/**
	 * This method initializes resetButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getResetButton() {
		if (resetButton == null) {
			resetButton = new JButton();
			resetButton.setBounds(new Rectangle(409, 428, 126, 24));
			resetButton.setText("Reset Metrics");
			resetButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					maxSize = 0;
					maxBufSizeLabel.setText(Long.toString(maxSize / 1000)
							+ " kb");
					maxPackets = 0;
					maxPacketsLabel.setText(Long.toString(maxPackets));
				}
			});
		}
		return resetButton;
	}

} // @jve:decl-index=0:visual-constraint="278,27"
